<template>
  <page-layout>
    <div slot="toolbar">
      <el-button size="small" round type="primary" @click="newCaseMethod">新增以案释法</el-button>
    </div>
    <div class="card-header">以案释法管理</div>
    <el-card class="box-card">
      <el-card class="box-card">
          <el-form ref="searchCaseMethodCondition" :model="searchCaseMethodCondition" label-width="80px" inline>
            <el-form-item label="标题">
              <el-input v-model="searchCaseMethodCondition.title" style="width: 300px;"/>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="searchCaseMethodCondition.status" placeholder="请选择">
                <el-option
                  v-for="item in CaseMethodStatus"
                  :key="item.value"
                  :label="item.text"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="search(1)">查询</el-button>
            </el-form-item>
          </el-form>
      </el-card>
      <div class="flex-row">
        <div class="flex-item">
          <el-card class="box-card">
            <el-table
              :data="caseMethodList.content" border stripe
              style="width: 100%">
              <el-table-column
                prop="id"
                label="编号"
                width="55">
              </el-table-column>
              <el-table-column prop="image" label="图片" width="210">
                <template slot-scope="scope">
                  <image-cover :thumb.sync="scope.row.image" width="187.5px" height="125px" size-type="contain"></image-cover>
                </template>
              </el-table-column>
              <el-table-column prop="title" label="标题" ></el-table-column>
              <el-table-column
                prop="provider"
                label="发布部门" width="120">
              </el-table-column>
              <el-table-column
                prop="status"
                label="状态" width="120">
                <template slot-scope="scope">
                  <span>{{scope.row.status| keyText('CaseMethodStatus')}}</span>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="220">
                <template slot-scope="scope">
                  <el-button size="mini" type="text"  @click="preCaseMethod(scope.row)">
                    预览
                  </el-button>
                  <el-button size="mini" v-if="scope.row.status===3||scope.row.status===2||scope.row.status===1" @click="editCaseMethod(scope.$index, scope.row)" type="text">编辑</el-button>
                  <el-button  size="mini" type="text"  v-if="scope.row.status===3" @click="save(scope.row,2)">提交审核</el-button>
                  <el-button  size="mini" type="text"  v-if="scope.row.status===3" v-has="{class:'case:publish'}" @click="save(scope.row,3)">发布</el-button>
                  <el-button  size="mini" type="text"  v-if="scope.row.status===4" v-has="{class:'case:reject'}" @click="save(scope.row,5)">驳回</el-button>
                  <el-button  size="mini" type="text"  v-if="scope.row.status===4" v-has="{class:'case:pass'}" @click="save(scope.row,4)">通过</el-button>
                  <el-button  size="mini" type="text"  v-if="scope.row.status===1" v-has="{class:'case:up'}" @click="save(scope.row,7)">上架</el-button>
                  <el-button  size="mini" type="text"  v-if="scope.row.status===5" v-has="{class:'case:down'}" @click="save(scope.row,6)">下架</el-button>

                  <el-button size="mini" type="text" @click="deleteCaseMethod(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <div class="mt10">
              <el-pagination
                background
                layout="prev, pager, next"
                :total="caseMethodList.totalElements"
                :page-size="caseMethodList.size"
                :current-page.sync="currentCaseMethodPage" @current-change="getPageChange">
              </el-pagination>
            </div>
          </el-card>
        </div>
      </div>
    </el-card>
    <el-dialog :visible.sync="showCaseMethodEditor" title="添加/编辑 以案释法" width="80%">
      <caseMethod-editor @close="onCaseMethodEditorClose" :is-close="showCaseMethodEditor"></caseMethod-editor>
    </el-dialog>
    <el-dialog title="预览" :visible.sync="showPreview" width="50%">
      <pre-case-method @close="onShowPreviewClose"  :is-close="showPreview"></pre-case-method>
    </el-dialog>
  </page-layout>
</template>
<script>
import PageLayout from "@/components/page-layout";
import CaseMethodEditor from "./caseMethod-editor";
import { mapGetters } from "vuex";
import ImageCover from "@/components/image-cover";
import PreCaseMethod from "./pre-caseMethod";
import Status from "@/libs/status";
export default {
  created() {
    this.$store.dispatch("getCaseMethodPageS").then(ret => {});
  },
  mounted() {},
  data() {
    return {
      currentCaseMethodPage: 1,
      showCaseMethodEditor: false,
      showPreview: false,
      CaseMethodStatus: Status.CaseMethodStatus
    };
  },
  computed: {
    ...mapGetters({
      caseMethodList: "caseMethodList",
      searchCaseMethodCondition: "searchCaseMethodCondition"
    })
  },
  methods: {
    getPageChange(val) {
      var tem = Object.assign(
        { pageIndex: val - 1 },
        this.searchCaseMethodCondition
      );
      this.$store.dispatch("getCaseMethodPageS", tem).then(ret => {});
    },
    search(val) {
      this.getPageChange(1);
    },
    deleteCaseMethod(row) {
      this.$confirm("确认删除当前以案释法?", "提示", {})
        .then(() => {
          this.$store
            .dispatch("deleteCaseMethodS", { id: row.id })
            .then(ret => {
              this.getPageChange(1);
              this.$notify({
                title: "删除成功",
                type: "success"
              });
            });
        })
        .catch(() => {});
    },
    preCaseMethod(row) {
      this.$store.dispatch("getCaseMethodS", row.id);
      this.showPreview = true;
    },
    newCaseMethod() {
      this.$store.commit("INIT_CURRENT_CASEMETHOD");
      this.showCaseMethodEditor = true;
    },
    editCaseMethod(index, row) {
      this.$store.dispatch("getCaseMethodS", row.id);
      this.showCaseMethodEditor = true;
    },
    onCaseMethodEditorClose() {
      this.showCaseMethodEditor = false;
      this.getPageChange(1);
    },
    save(row, status) {
      row.opt = status;
      this.$store.dispatch("addOrUpdateCaseMethodS", row).then(ret => {
        this.$notify({
          title: "提交成功",
          type: "success"
        });
        this.getPageChange(1);
      });
    },
    onShowPreviewClose() {
      this.showPreview = false;
      this.getPageChange(1);
    }
  },
  components: {
    PageLayout,
    CaseMethodEditor,
    ImageCover,
    PreCaseMethod
  }
};
</script>
<style>
</style>
